﻿using System;
using MS.InformationManagement.Presenter.SearchRefinementPanel;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls;
using Microsoft.SharePoint;
using MS.InformationManagement.Controls;
using System.Web.UI.WebControls;

namespace MS.InformationManagement.WP.DocumentCenter.SearchRefinementPanel
{
    public partial class SearchRefinementPanelUserControl : BaseControl, ISearchRefinementPanelView
    {
        SearchRefinementPanelPresenter _Presenter;

        #region [Event Handlers]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                _Presenter.FetchSearchHistory(SPContext.Current.Web.CurrentUser.ID);
                Pager1.BuildPager();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _Presenter = new SearchRefinementPanelPresenter(this);
            Pager1.OnPageChanged += new Pager.PageChangeHandler(OnPageChanged);
            //Pager1.NextLinkText = ">>";
            //Pager1.PreviousLinkText = "<<";
            //PageSize = 5;
            //if (!Page.IsPostBack)
            //{
            //    //Pager1.NumberOfPagesToDisplay = 5;
                
            //    PageIndex = 0;
            //}
            
        }

        protected void OnPageChanged(object sender, PageChangeEventArgs e)
        {
            _Presenter.FetchSearchHistory(SPContext.Current.Web.CurrentUser.ID);
            Pager1.BuildPager();
        }

        protected void OnFilterClick(object sender, FilterClickEventArgs e)
        {
            SelectedSearchHistoryItem = e.Filter;
        }

        protected void rpt_History_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            if (e.Item.DataItem != null)
            {
                SearchHistoryTemplate currentTemplate = e.Item.FindControl("SearchHistoryTemplate1") as SearchHistoryTemplate;
                MS.InformationManagement.DAL.DocumentCenter.HistorialDeBusqueda currentItem = e.Item.DataItem as MS.InformationManagement.DAL.DocumentCenter.HistorialDeBusqueda;

                currentTemplate.SearchDate = currentItem.FechaDeBúsqueda;
                currentTemplate.SearchType = currentItem.SearchConfig.SearchType;
                currentTemplate.SimpleSearchCriteria = currentItem.SearchConfig.SimpleSearchCriteria;
                currentTemplate.AdvancedSearchCriteria = currentItem.SearchConfig.AdvancedSearchCriteria;
                currentTemplate.SearchConfig = currentItem.SearchConfig;
            }
        }
        #endregion

        #region [ISearchRefinementPanelView members]
        public System.Collections.Generic.IEnumerable<MS.InformationManagement.DAL.DocumentCenter.HistorialDeBusqueda> SearchHistoryCollection
        {
            get
            {
                return rpt_History.DataSource as System.Collections.Generic.IEnumerable<MS.InformationManagement.DAL.DocumentCenter.HistorialDeBusqueda>;
            }
            set
            {
                rpt_History.DataSource = value;
                rpt_History.DataBind();
            }
        }

        public int PageIndex
        {
            get { return Pager1.PageIndex; }
            set { Pager1.PageIndex = value; }
        }

        public int PageSize
        {
            get { return Pager1.PageSize; }
            set { Pager1.PageSize = value; }
        }

        public int TotalRecords
        {
            get { return Pager1.TotalRecords; }
            set { Pager1.TotalRecords = value; }
        }

        public DAL.DocumentCenter.SearchConfig SelectedSearchHistoryItem
        {
            get;
            set;
        }
        #endregion

        protected void rpt_History_ItemCreated(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            SearchHistoryTemplate currentTemplate = e.Item.FindControl("SearchHistoryTemplate1") as SearchHistoryTemplate;
            currentTemplate.OnFilterClick += new SearchHistoryTemplate.FilterClickHandler(OnFilterClick);
        }
    }
}
